<?php

    require_once("usuario.php");
    require_once("playlists.php");
    require_once("SmartyReady.php");
    // preparem la sessio
    session_start();
    // preparem l'smarty
    $sm = new SmartyReady();
    $sm->muntarTemplates();
    $sm->assign('get2',false);
    if (!empty($_SESSION['username'])) {
        $sm->assign('banner','bannerUser.tpl');
        $sm->assign('usuari',$_SESSION['username']);
        $sm->assign('pathAvatar',$_SESSION['avatar']);
        $sm->assign('cuerpo','reproPlaylist.tpl');
        if (empty($_GET['ppl'])) {
            $luser = Usuario::findByUsername($_SESSION['username']);
            $lid = $luser->get_id();
            // Busquem les playlists de l'usuari i les parsejem
            $array_playlists = Playlists::findbyidusuari($lid);
            $noms_playlists = Array();
            $idsplaylists = Array();
            $sm->assign('nomplay',"");
            $sm->assign('get2',true);
            if(count($array_playlists) > 0) {
                foreach($array_playlists as $pl) {
                    $noms_playlists[] = $pl->get_nom();
                    $idsplaylists[$pl->get_nom()] = $pl->get_id();
                }
                $taula = '<table>';
                $index = 0;
                while ($index < count($noms_playlists)) {
                    $taula .= '<tr><td><a href="./reproPlaylist.php?ppl=' .
                               $idsplaylists[$noms_playlists[$index]] . '">' . $noms_playlists[$index] . '</a></td></tr>';
                    $index++;
                }
                $taula .= '</table>';
                $sm->assign('pls',true);
                $sm->assign('taula',$taula);
            }
            else $sm->assign('pls',false);
        }
        else {
            $sm->assign('pls',false);
            $pl = new Playlists($_GET['ppl']);
            $playlist = $pl->songs();
            // Agafem les cançons de la playlist
            $playlist_raw = Array();
            $playlist_tpl = Array();
            $index = 0;
            if (count($playlist) > 0) {
                foreach($playlist as $song) {
                    $playlist_raw[]['title'] = $song->get_titol();
                    $playlist_raw[$index]['author'] = $song->get_artista();
                    $playlist_raw[$index]['file'] = $song->get_file();
                    $playlist_tpl[] = '<a href="#" data-src="' . substr($playlist_raw[$index]['file'],11) . '">' .
                                                                 $playlist_raw[$index]['title'] . '</a>';

                    $index++;
                }
                $sm->assign('playlist',$playlist_tpl);
            }
            else $sm->assign('playlist','');
        }
}
else {
        // T'has colat a la pagina del perfil sense estar logejat
        $sm->assign("banner","login.tpl");
        $sm->assign('resultatLogin','');
        $sm->assign('cuerpo','buit.tpl');
        ?><meta http-equiv="Refresh" content="0;url=./index.php"><?php
}
$sm->display("E:/wamp/www/JediMusic/Smarty/templates/base.tpl");
?>
